package com.shinewind.info.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.shinewind.common.constant.messageConstant;
import com.shinewind.home.service.IBrandService;
import com.shinewind.home.service.IModelService;
import com.shinewind.home.service.IStatusService;
import com.shinewind.info.service.*;
import com.shinewind.home.service.IRateService;
import com.shinewind.pojo.dto.ModelCommentDto;
import com.shinewind.pojo.dto.ParentCommentDto;
import com.shinewind.pojo.entity.Model;
import com.shinewind.pojo.entity.ModelInfo;
import com.shinewind.pojo.result.Result;
import com.shinewind.pojo.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.List;

@RestController
@RequestMapping("/modelDetail")
@Api(value = "评测接口",description = "用于展示模型的评测文章等信息")
public class DetailController {
    @Autowired
    private IModelInfoService iModelInfoService;
    @DubboReference
    private IModelService iModelService;
    @DubboReference
    private IStatusService iStatusService;
    @DubboReference
    private IBrandService iBrandService;
    @DubboReference
    private IRateService iRateService;
    @Autowired
    private IModelTypeService iModelTypeService;
    @Autowired
    private IModelTypeContentService iModelTypeContentService;
    @Autowired
    private IModelFirmContentService iModelFirmContentService;

    //生成分享的二维码
    @GetMapping("/detail/qrCode")
    @ApiOperation(value = "生成二维码", notes = "生成二维码", response = void.class)
    public void getQrCode(HttpServletResponse resp) throws Exception {
        //生成二维码图片
        QrConfig config = new QrConfig(100,100);
        // 高纠错级别
        config.setErrorCorrection(ErrorCorrectionLevel.H);
        config.setMargin(2);
        //设置浏览器输出为图片
        //resp.setContentType("image/jpeg");
        //禁止浏览器缓存
        resp.setHeader("Pragma","No-cache");
        //输出到浏览器
        OutputStream outputStream = resp.getOutputStream();
        BufferedImage bufferedImage = QrCodeUtil.generate("http://www.baidu.com",
                config);
        ImageIO.write(bufferedImage,"JPEG",outputStream);
        outputStream.flush();
        outputStream.close();
    }

    @GetMapping("/{mId}/{uId}")
    @ApiOperation(value = "查询模型详细信息", notes = "查询模型详细信息", response = Result.class)
    public Result getModelInfoBymId(@PathVariable("mId") String mId,@PathVariable("uId") String uId){
        ModelInfo modelInfo = iModelInfoService.getModelInfoBymId(mId);
        ModelDetailVo modelDetailVo = ModelDetailVo.builder().build();
        BeanUtils.copyProperties(modelInfo,modelDetailVo);
        //查询模型标题、展示图、价格
        Model model = iModelService.getModelByMId(mId);
        BeanUtils.copyProperties(model,modelDetailVo);
        modelDetailVo.setTitle(model.getGoodsName());
        //查询评分
        //如果该模型评分总条数大于0
        if(iRateService.getCountAndRateBymId(mId) > 0){
            //返回该模型的平均评分
            modelDetailVo.setRate(iRateService.getSumAndRateBymId(mId) / iRateService.getCountAndRateBymId(mId));
            //判断当前用户是否已有评分记录
            System.out.println("uid:"+uId);
            System.out.println("myRate:"+iRateService.getRateByuId(uId,mId));
            if(iRateService.getCountAndRateByuId(uId) > 0){
                modelDetailVo.setMyRate(iRateService.getRateByuId(uId,mId));
            }
            //如果当前用户没有评分记录
            else{
                modelDetailVo.setMyRate(0.0);
            }
        }
        //如果该模型没有评分
        else{
            modelDetailVo.setRate(0.0);
            modelDetailVo.setMyRate(0.0);
        }
        //查询发售时间和品牌
        modelDetailVo.setSaleTime(iStatusService.getStatusInfoById(mId));
        modelDetailVo.setBrand(iBrandService.getBrandInfoById(model.getBId()).getBrand());
        return Result.success(modelDetailVo, ObjectUtil.isNotNull(modelDetailVo),
                messageConstant.QUERY_SUCCESS);
    }

    @GetMapping("/pic/{mId}")
    @ApiOperation(value = "查询模型说明书、官图详细信息", notes = "查询模型详细信息", response = Result.class)
    public Result getModelPicBymId(@PathVariable("mId") String mId){
        ModelInfo modelInfo = iModelInfoService.getModelInfoBymId(mId);
        ModelDetailPicVo modelDetailPicVo = ModelDetailPicVo.builder().build();
        BeanUtils.copyProperties(modelInfo,modelDetailPicVo);
        return Result.success(modelDetailPicVo, ObjectUtil.isNotNull(modelDetailPicVo),
                messageConstant.QUERY_SUCCESS);
    }

    @GetMapping("/series/{typeId}")
    @ApiOperation(value = "查询模型系列信息", notes = "查询模型系列信息", response = Result.class)
    public Result getModelSeriesByTypeId(@PathVariable("typeId") Long typeId){
        //根据typeId获取类别名称
        String typeName = iModelTypeService.getTypeNameByTypeId(typeId);
        //获取系列分类
        List<String> sortTitle = iModelTypeContentService.getSortTitleByTypeId(typeId);
        //根据typeId和系列分类来填充返回的系列内容列表
        List<ModelSeriesVo> modelSeriesVos = iModelTypeContentService.getSeriesContentList(typeId,sortTitle);
        return Result.success(modelSeriesVos, BooleanUtil.isTrue(modelSeriesVos.size() > 0),
                messageConstant.QUERY_SUCCESS);
    }

    @GetMapping("/firms/{typeId}")
    @ApiOperation(value = "查询模型厂商信息", notes = "查询模型厂商信息", response = Result.class)
    public Result getModelFirmsByTypeId(@PathVariable("typeId") Long typeId){
        //获取厂商分类
        List<String> sortTitle = iModelFirmContentService.getSortTitleByTypeId(typeId);
        //根据typeId和厂商分类来填充返回的厂商内容列表
        List<ModelFirmsVo> modelFirmsVos = iModelFirmContentService.getFirmsContentList(typeId,sortTitle);
        return Result.success(modelFirmsVos, BooleanUtil.isTrue(modelFirmsVos.size() > 0),
                messageConstant.QUERY_SUCCESS);
    }

    @GetMapping("/video/{mId}")
    @ApiOperation(value = "查询模型相关视频", notes = "查询模型视频信息", response = Result.class)
    public Result getRelateVideoByDid(@PathVariable("mId") String mId){
        ModelInfo modelInfo = iModelInfoService.getModelInfoBymId(mId);
        System.out.println("modelInfo:"+modelInfo);
        System.out.println("mId:"+mId);
        ModelVideoVo modelVideoVo = ModelVideoVo.builder().build();
        BeanUtils.copyProperties(modelInfo,modelVideoVo);
        modelVideoVo.setTitle(iModelService.getModelByMId(mId).getTitle());
        return Result.success(modelVideoVo,true,messageConstant.QUERY_SUCCESS);
    }
}
